#!/usr/bin/env bash
set -eu

#"OK": 0
#"WARNING": 1
#"CRITICAL": 2
#"UNKNOWN": 3

# checks if the user is root, and if not, try to re-exec.
if [ "$(id -u)" = "0" ]; then
    su zulip -c "$0"
    exit 0
fi

cd /home/zulip/deployments/current
BACKLOG="$(./manage.py print_email_delivery_backlog)"

if [ "$BACKLOG" -gt 0 ] && [ "$BACKLOG" -lt 10 ]; then
    echo "backlog of $BACKLOG"
    exit 1
elif [ "$BACKLOG" -ge 10 ]; then
    echo "backlog of $BACKLOG"
    exit 2
else
    echo "no backlog"
    exit 0
fi
